﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XML_Programming_Assignment.Class;

namespace XML_Programming_Assignment
{
    public partial class FrmReadQuotation : Form
    {
        public FrmReadQuotation()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {        
            string filepath = string.Empty;
            dialogXML.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            dialogXML.Title = "Select an XML file";

            if (dialogXML.ShowDialog() == DialogResult.OK)
            {
                tbxUrl.Clear();
                tbxQuotationInfo.Clear();

                filepath = dialogXML.FileName;
                tbxUrl.Text = filepath;
                DBManager.readQuotation(filepath);
                //ReadXML(strFilename);

                if (DBManager.quotationList_1.Count != 0 || DBManager.quotationItemList_1.Count != 0)
                {

                    foreach (Quotation quotation in DBManager.quotationList_1)
                    {
                        tbxQuotationInfo.Text += "Quotation Number : " + quotation.QuotationNum + "\r\n";
                        tbxQuotationInfo.Text += "Quotation Date : " + quotation.QuotationDate + "\r\n";
                        tbxQuotationInfo.Text += "RFQ ID : " + quotation.RFQID + "\r\n";
                        tbxQuotationInfo.Text += "Seller ID : " + quotation.SellerID + "\r\n" + "\r\n";

                        foreach (QuotationItem quotationItem in DBManager.quotationItemList_1)
                        {

                            if (quotation.QuotationNum == quotationItem.QuotationNum)
                            {
                                tbxQuotationInfo.Text += "Product ID : " + quotationItem.ProductID + "\r\n";
                                tbxQuotationInfo.Text += "Quantity : " + quotationItem.Quantity + "\r\n";
                                tbxQuotationInfo.Text += "Unit Price : " + quotationItem.UnitPrice + "\r\n";
                                tbxQuotationInfo.Text += "Total Price : " + quotationItem.TotalPrice + "\r\n" + "\r\n";
                            }
                        }
                        tbxQuotationInfo.Text += "Item total prices : " + quotation.ItemTotalPrice + "\r\n";
                        tbxQuotationInfo.Text += "Shipping Charges : " + quotation.ShippingCharges + "\r\n";
                        tbxQuotationInfo.Text += "Quotation Total : " + quotation.QuotationTotal + "\r\n" + "\r\n";
                        tbxQuotationInfo.Text += "----------------- End of Quotation ---------------";
                    }
                    btnSave.Visible = true;
                }
                else
                {
                    MessageBox.Show("Incorrect XML file", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                
            }

            if (filepath == String.Empty)
            {
                MessageBox.Show("Nothing is chosen", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxUrl.Clear();
                tbxQuotationInfo.Clear();
                return;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (DBManager.quotationList_1.Count == 0 && DBManager.quotationItemList_1.Count == 0)
            {
                MessageBox.Show("Quotation are not saved into database", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DBManager.saveQuotation();
                MessageBox.Show("Quotations are saved into database successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxUrl.Clear();
                tbxQuotationInfo.Clear();
            }            
        }

        private void MainMenu_toolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            AllFormClass.formDefault.Show();
        }

        private void ReadQuotation_toolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            AllFormClass.formReadQuotation.Show();
        }

        private void Validate_toolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            AllFormClass.formRFQValidation.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("David Computer Service Pte Ltd" + "\r\n" + "Supply Chain Management System" + "\r\n" + "Developed By" + "\r\n" + "Sithu Kyaw", "Version", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void notepadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Notepad.exe");
        }

        private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc");
        }
    }
}
